<!--评论模块-->
<template>
  <div class="container detailBox tcommonBox" style="max-width: 100%">
    <div class="tmsg-respond"  ref="respondBox">
      <h3>发表评论 </h3>
      <form class=""  >
        <el-input
          type="textarea"
          :rows="2"
          placeholder="说点什么呢``"
          v-model="textarea">
        </el-input>
        <el-row class="tmsg-r-info">
          <el-col :span="24" class="info-submit">
            <p class="tcolors-bg" @click="addComment">{{sendTip}}</p>
          </el-col>
        </el-row>
      </form>
    </div>

    <div class="comment" v-for="item in comments">
      <div class="info">
        <img class="comment-avatar" :src="item.fromAvatar===null?$store.state.errorImg:item.fromAvatar"  width="36" height="36"/>
        <div class="right">
          <div class="name">{{item.fromName ===null?"游客":item.fromName}}</div>
          <div class="date">{{item.date}}</div>
        </div>
      </div>
      <div class="content">{{item.content}}</div>
      <div class="control">
        <span class="like" :class="{active: item.likeFlag}" @click="likeClick(item)">
          <i class="iconfont icon-like"></i>
          <span class="like-num">{{item.likeNum > 0 ? item.likeNum + '人赞' : '赞'}}</span>
        </span>
        <span class="comment-reply" @click="showCommentInput(item)">
          <i class="iconfont icon-comment"></i>
          <span>回复</span>
        </span>
      </div>
      <div class="reply">
        <div class="item" v-for="reply in item.reply">
          <div class="reply-content">
            <span class="from-name">{{reply.fromName===null?"游客":reply.fromName}}</span><span>: </span>
            <span class="to-name" v-if="reply.toName!==null">@{{reply.toName}}</span>
            <span>{{reply.content}}</span>
          </div>
          <div class="reply-bottom">
            <span>{{reply.date}}</span>
            <span class="reply-text" @click="showCommentInput(item, reply)">
              <i class="iconfont icon-comment"></i>
              <span>回复</span>
            </span>
          </div>
        </div>
        <div class="write-reply" v-if="item.reply!==null" @click="showCommentInput(item)">
          <i class="el-icon-edit"></i>
          <span class="add-comment">添加新评论</span>
        </div>
        <transition name="fade">
          <div class="input-wrapper" v-if="showItemId === item.id">
            <el-input class="gray-bg-input"
                      v-model="inputComment"
                      type="textarea"
                      :rows="3"
                      autofocus
                      :placeholder="toNickname">
            </el-input>
            <div class="btn-control">
              <span class="cancel" @click="cancel">取消</span>
              <el-button class="btn" type="success" round @click="replyComment(item)">确定</el-button>
            </div>
          </div>
        </transition>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    props: {
      comments: {
        type: Array,
        required: true
      }
    },
    components: {},
    data() {
      return {
        textarea:'',// 新增留言的输入框
        inputComment: '',
        showItemId: '',
        sendTip: '评论',
        toNickname:'',
        currentReply:{}
      }
    },
    computed: {},
    methods: {
      /**
       * 点赞
       */
      likeClick(item) {
        if(this.$store.state.userId ===-1){
          this.$message.info("登录后即可点赞评论！")
        }else{
          if (item.likeFlag) {
            item.likeNum--
          } else {
            item.likeNum++
          }
          item.likeFlag = !item.likeFlag;
          this.$emit('likeClick', item)
        }
      },

      /**
       * 点击取消按钮
       */
      cancel() {
        this.showItemId = ''
      },

      /**
       * 回复评论
       * inputComment:回复内容
       * item: 一级评论
       * currentReply: 二级评论
       */
      replyComment(item) {
        this.$emit('reply',this.inputComment,item,this.currentReply)
        this.closeCommentInput()
      },


      /**
       * 新增评论
       */
      addComment(){
        this.$emit('add', this.textarea)
        this.closeCommentInput()
      },

      /**
       * 关闭回复窗口并清空内容
       */
      closeCommentInput(){
        this.inputComment = ''
        this.showItemId = ''
      },

      /**
       * 点击评论按钮显示输入框
       * item: 当前大评论
       * reply: 当前回复的评论
       */
      showCommentInput(item, reply) {
        if (reply && reply.fromName!==null) {
          this.toNickname = "@" + reply.fromName + ":"
        } else {
          this.toNickname = '写下你的评论'
        }
        this.currentReply = reply
        this.showItemId = item.id
      }
    },
    created() {
      console.log(this.comments)
    }
  }
</script>

<style scoped lang="scss">

  $color-main: #409EFF;
  $color-success: #67C23A;
  $color-warning: #E6A23C;
  $color-danger: #F56C6C;
  $color-info: #909399;

  $text-main: #303133;
  $text-normal: #606266;
  $text-minor: #909399; //次要文字
  $text-placeholder: #C0C4CC;
  $text-333: #333;

  $border-first: #DCDFE6;
  $border-second: #E4E7ED;
  $border-third: #EBEEF5;
  $border-fourth: #F2F6FC;

  $content-bg-color: #fff;

  .container {
    padding: 0 10px;
    box-sizing: border-box;

    .comment {
      display: flex;
      flex-direction: column;
      padding: 10px;
      border-bottom: 1px solid $border-fourth;

      .info {
        display: flex;
        align-items: center;

        .comment-avatar {
          border-radius: 50%;
        }

        .right {
          display: flex;
          flex-direction: column;
          margin-left: 10px;

          .name {
            font-size: 16px;
            color: $text-main;
            margin-bottom: 5px;
            font-weight: 500;
          }

          .date {
            font-size: 12px;
            color: $text-minor;
          }
        }
      }

      .content {
        font-size: 16px;
        color: $text-main;
        line-height: 20px;
        padding: 10px 0;
      }

      .control {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: $text-minor;

        .like {
          display: flex;
          align-items: center;
          margin-right: 20px;
          cursor: pointer;

          &.active, &:hover {
            color: $color-main;
          }

          .iconfont {
            font-size: 14px;
            margin-right: 5px;
          }
        }

        .comment-reply {
          display: flex;
          align-items: center;
          cursor: pointer;

          &:hover {
            color: $text-333;
          }

          .iconfont {
            font-size: 16px;
            margin-right: 5px;
          }
        }

      }

      .reply {
        margin: 10px 0;
        border-left: 2px solid $border-first;

        .item {
          margin: 0 10px;
          padding: 10px 0;
          border-bottom: 1px dashed $border-third;

          .reply-content {
            display: flex;
            align-items: center;
            font-size: 14px;
            color: $text-main;

            .from-name {
              color: $color-main;
            }

            .to-name {
              color: $color-main;
              margin-left: 5px;
              margin-right: 5px;
            }
          }

          .reply-bottom {
            display: flex;
            align-items: center;
            margin-top: 6px;
            font-size: 12px;
            color: $text-minor;

            .reply-text {
              display: flex;
              align-items: center;
              margin-left: 10px;
              cursor: pointer;

              &:hover {
                color: $text-333;
              }

              .icon-comment {
                margin-right: 5px;
              }
            }
          }
        }

        .write-reply {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: $text-minor;
          padding: 10px;
          cursor: pointer;

          &:hover {
            color: $text-main;
          }

          .el-icon-edit {
            margin-right: 5px;
          }
        }

        .fade-enter-active, fade-leave-active {
          transition: opacity 0.5s;
        }

        .fade-enter, .fade-leave-to {
          opacity: 0;
        }

        .input-wrapper {
          padding: 10px;

          .gray-bg-input, .el-input__inner {
            /*background-color: #67C23A;*/
          }

          .btn-control {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            padding-top: 10px;

            .cancel {
              font-size: 16px;
              color: $text-normal;
              margin-right: 20px;
              cursor: pointer;

              &:hover {
                color: $text-333;
              }
            }

            .confirm {
              font-size: 16px;
            }
          }
        }
      }
    }
  }

  //整合temMessage
  .tmsg-respond h3{
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 8px;
  }
  .tmsg-respond h3 small{
    font-size: smaller;
    cursor: pointer;
  }
  .tmsg-respond textarea{
    background:#f4f6f7;
    height:100px;
    margin-bottom: 10px;
  }

  .tmsg-r-info{
    margin:10px 0;
  }
  .tmsg-r-info input{
    height:30px;
    border-radius: 4px;
    background: #f4f6f7;
  }
  .tmsg-r-info .info-submit{
    margin: 10px 0;
    text-align: center;
  }
  .tmsg-r-info .info-submit p,.tmsg-commentshow h1{
    /*background: #97dffd;*/
    color:#fff;
    border-radius: 5px;
    cursor: pointer;
    /*transition: all .3s ease-in-out;*/
    height:30px;
    line-height: 30px;
    text-align: center;
  }
</style>
